/**
 * 
 */
package com.alkandros.descuentosperu;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.alkandros.descuentosperu.constants.IAppConstants;

import fragments.WebViewFragment;

/**
 * @author Jiju Induchoodan
 *
 */
public class WebViewActivity extends Activity  implements IAppConstants {

	private DrawerLayout mDrawerLayout;
	private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;
	
	private CharSequence mDrawerTitle;
    private CharSequence mTitle;
	private String [] urls;
	private String [] items;
	private int pos;
	WebViewFragment fragment;
	private ArrayList<Integer> posArray = new ArrayList<Integer>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_webview);
		Bundle extras= getIntent().getExtras();
		if(extras!=null)
		{
			urls  = extras.getStringArray(URLS);
			items = extras.getStringArray(ITEMS);
			pos   = extras.getInt(POSITION);
		}
		
		initialiseUI(savedInstanceState);
		
		/**/
	}
	
	private void initialiseUI(Bundle savedInstanceState)
	{
		 mTitle = mDrawerTitle = getTitle();
		 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
	     mDrawerList = (ListView) findViewById(R.id.left_drawer);
	     
	     mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
	     mDrawerList.setAdapter(new ArrayAdapter<String>(this,
	                R.layout.drawer_list_item, items));
	     mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
	     
	     getActionBar().setDisplayHomeAsUpEnabled(true);
	        getActionBar().setHomeButtonEnabled(true);

	        // ActionBarDrawerToggle ties together the the proper interactions
	        // between the sliding drawer and the action bar app icon
	        mDrawerToggle = new ActionBarDrawerToggle(
	                this,                  /* host Activity */
	                mDrawerLayout,         /* DrawerLayout object */
	                R.drawable.ic_drawer,  /* nav drawer image to replace 'Up' caret */
	                R.string.drawer_open,  /* "open drawer" description for accessibility */
	                R.string.drawer_close  /* "close drawer" description for accessibility */
	                ) {
	            public void onDrawerClosed(View view) {
	                getActionBar().setTitle(mTitle);
	                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
	            }

	            public void onDrawerOpened(View drawerView) {
	       //         getActionBar().setTitle(mDrawerTitle);
	                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
	            }
	        };
	        mDrawerLayout.setDrawerListener(mDrawerToggle);

	        if (savedInstanceState == null) {
	            selectItem(pos);
	        }
	     
	}
	 private class DrawerItemClickListener implements ListView.OnItemClickListener {
	        @Override
	        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
	        	posArray.add(0,pos);
	            selectItem(position);
	            pos=position;
	        }
	    }
	
	 @Override
	    protected void onPostCreate(Bundle savedInstanceState) {
	        super.onPostCreate(savedInstanceState);
	        // Sync the toggle state after onRestoreInstanceState has occurred.
	        mDrawerToggle.syncState();
	    }

	    @Override
	    public void onConfigurationChanged(Configuration newConfig) {
	        super.onConfigurationChanged(newConfig);
	        // Pass any configuration change to the drawer toggls
	        mDrawerToggle.onConfigurationChanged(newConfig);
	    }
	    
	    
	    @Override
	    public boolean onOptionsItemSelected(MenuItem item) {
	         // The action bar home/up action should open or close the drawer.
	         // ActionBarDrawerToggle will take care of this.
	        if (mDrawerToggle.onOptionsItemSelected(item)) {
	            return true;
	        }
	        if(item.getItemId()==R.id.action_refresh)
	        {
	        	 fragment.webView.loadUrl("javascript:window.location.reload( true )");
	        }
	        else if(item.getItemId()==R.id.action_back)
	        {
	        	if(posArray.size()>0)
	        	{
	        		selectItem(posArray.get(0));
		        	posArray.remove(posArray.get(0));
	        	}
	        	else
	        	{
	        		super.onBackPressed();
	        	}
	        }
			return true;
	    }
	 private void selectItem(int position) {
	        // update the main content by replacing fragments
	        fragment = new WebViewFragment();
	        Bundle args = new Bundle();
	        args.putInt(WebViewFragment.ARG_NUMBER, position);
	        args.putStringArray(ITEMS, items);
	        args.putStringArray(URLS, urls);
	        fragment.setArguments(args);

	        FragmentManager fragmentManager = getFragmentManager();
	        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

	        // update selected item and title, then close the drawer
	        mDrawerList.setItemChecked(position, true);
	        setTitle(items[position]);
	        mDrawerLayout.closeDrawer(mDrawerList);
	    }
	 @Override
	    public boolean onCreateOptionsMenu(Menu menu) {
	        MenuInflater inflater = getMenuInflater();
	        inflater.inflate(R.menu.main, menu);
	        return super.onCreateOptionsMenu(menu);
	    }
	 @Override
	protected void onDestroy() {
		super.onDestroy();
		if(posArray!=null)
			posArray.clear();
	}

	@Override
	    public void setTitle(CharSequence title) {
	        mTitle = title;
	        getActionBar().setTitle(mTitle);
	    }

	public static void openActivity(Context context,String[] urls,String [] items,int pos)
	{
		Intent intent = new Intent(context,WebViewActivity.class);
		intent.putExtra(URLS, urls);
		intent.putExtra(ITEMS, items);
		intent.putExtra(POSITION, pos);
		context.startActivity(intent);
	}

}
